function anchorPraser(){
	if(getAnchor().indexOf('@') >=0 ){
		var para = getAnchor().split('@');
		return d = {
			'room_id':para[0],//'password':para[1],
		}
	}else{
		var para = getQuery();
		return d= {
			'room_id':para['room_id'],//'password':para[1],
			//'room_id': idNumberCoder.decode(para['room_id_key']);
		}
	}
}

/********** OPTIONS operator **********/

function addNewOption(i,inp){
	var model = document.getElementById('option-');
	
	var index = inp['item_id'] || i;
	var newnode = model.cloneNode(true);
	newnode.id = 'option-' + index;
	var namenode = newnode.getElementsByClassName('opname')[0];
	namenode.innerHTML = inp.content;
	namenode.id = 'name-'+index;
	///
	var valuenode = newnode.getElementsByClassName('opvalue')[0];
	valuenode.id = 'value-'+index;
	valuenode.name = 'items['+index+'][value]';
	///
	var opidnode = newnode.getElementsByClassName('opid')[0];
	opidnode.id = 'opid-'+index;
	opidnode.value = index;
	opidnode.name = 'items['+index+'][item_id]';
	//valuenode.innerHTML = inp.time;
	var slidernode = newnode.getElementsByClassName('opslider')[0];
	slidernode.id = 'slider-'+index;
	//
	var tipnode = newnode.getElementsByClassName('optip')[0];
	tipnode.id = 'tip-'+index;

	document.getElementById('poll-list').appendChild(newnode);

	var getOnChangeCallback = function(i){
		return function(rawValue, sliderObj) {
			proValue = valueProcess(rawValue);
			document.getElementById('tip-'+i).innerHTML = proValue;
			document.getElementById('slider-'+i).getElementsByClassName('dhxsl_runner')[0].dataset.value = proValue;
		}
	}

	var valueProcess = function(v){return Math.round(v/100*20)*5;}

	if(parseInt(rawData['room_type']) == 2){
		var mySlider = new dhtmlXSlider({
			parent:     'slider-'+index,
			size:       260,
			value:      50,
			step:       1,
			min:        0,
			max:        100,
			linkTo:     'value-'+index,
		});
		mySlider.attachEvent("onChange", getOnChangeCallback(index));
		//mySlider.attachEvent("onMouseDown",);
		mySlider.attachEvent("onMouseUp",function(index){
			var val = this.getValue();
			this.setValue(valueProcess(val));
		});
	}
}

function switcherToggle(el,opid){
	if(parseInt(rawData['room_type'])==0){
		setAllOptionValuesZero();//单选重置
	}
	if(opid===undefined){
		var pn = el.parentNode;
		var opval_input = pn.getElementsByClassName('opvalue')[0];
		if(el.classList.toggle('checked')){
			opval_input.value = '100';
		}else {
			opval_input.value = '0';
		}
	}else{
		//Do something later ..
	}
}

function setAllOptionValuesZero(){
	var elc = document.getElementsByClassName('opvalue');
	for(var i=0; i<elc.length; i++){
		elc[i].value = '0';
	}
	var elc = document.getElementsByClassName('switcher');
	for(var i=0; i<elc.length; i++){
		elc[i].classList.remove('checked');
	}
}

function loadOptionData(){
	getOptionData();
}



/**********Parese option data**********/

function getOptionData(requsetData){
	var para = anchorPraser();
	url = API_URL+'?room_id='+para['room_id']+'&type=2';
	getJSON(url,getOptionCallback);
	loadingBox.show();
}

function getOptionCallback(data,status){
	//alert(status);
	if(status != 'success'){
		alert('NO_RESPONSE_SERVER');
		return false;
	}
	if(data.error){
		alert('Here are some errors!\nError Message: ' + data.error);
		return false;
	}

	/*if(data['is_over'] || data.items[0]['is_over']){
		msgBox.pop({
			title:'投票已结束',
			content:'本投票已于'+getTime(data['end_time'])+'截止',
			buttons:[
				{text:'查看结果',link:'result.htm#'+anchorPraser()['room_id']+'@'}
			],
			icon:'sadsmile',
		});
		return;
	}

	if(data['is_over'] || data.items[0]['is_over']){
		msgBox.pop({
			title:'很不巧',
			content:'你已经投过票了',
			buttons:[
				{text:'查看投票结果', link:'result.htm#'+anchorPraser()['room_id']+'@'},
				{text:'再看一眼', action:'msgBox.hide();'}
			],
			icon:'sadsmile',
		});
		return;
	}

*/

	window.rawData = data;

	document.getElementById('poll-title').innerHTML = data.theme;
	document.getElementById('poll-description').innerHTML = data['theme_description'];
	document.getElementById('poll-time').innerHTML = getTime(data['end_time']);
	document.getElementById('poll-room').value = anchorPraser()['room_id'];
	/*if(data['is_viewable']){
		document.getElementById('view-result').href = 'result.htm#'+anchorPraser()['room_id']+'@';
	}else {
		document.getElementById('view-result').style.display = 'none';
	}*/
	
	//Room Type 投票类型相关 包括 提示文本 上方文字显示 
	var room_type = parseInt(data['room_type']);
	var classNameMap = ['poll-type-zero','poll-type-one','poll-type-two'];
	document.getElementById('poll-form').classList.add(classNameMap[room_type]);
	var textNameMap =  ['单选','多选','权重选'];
	document.getElementById('vote-type').innerHTML = textNameMap[room_type];

	setOption(data.items);
	loadingBox.hide();
}

function setOption(data){
	for(var i=0;i<data.length;i++){
		addNewOption(i+1,data[i]);
	}
}




/******** PollData Sender ********/

function sendFormPost(){
	var postData = $('#poll-form').serialize();
	postJSON(API_URL,postData,sendPostCallback,sendPostError);
	loadingBox.show();
}

function sendPostError(xhr){
	loadingBox.hide();
	var returnText = JSON.parse(xhr.responseText)['error'];
	msgBox.pop({
		title:'投票失败',
		content:'一个人只能投一次',
		buttons:[
				{text:'查看投票结果',link:'result.htm#'+anchorPraser()['room_id']+'@'}
		],
		'icon':'sadsmile',
	});
}

function sendPostCallback(data,status){
	loadingBox.hide();
	//if(data['error']){msgbox(data.error);return false;}
	//console.log(data);
	//document.getElementById('poll-success').style.display = 'block';
	//Show / Hide View-Result Btn
	url = API_URL+'?room_id='+anchorPraser()['room_id']+'&type=2';
	getJSON(url,function(data){
		var allowedMsg = document.getElementById('view-result-allowed-message');
		var forbidenMsg = document.getElementById('view-result-forbiden-message');
		var viewBtn = document.getElementById('view-result');
		if(data['is_viewable']){
			viewBtn.href = 'result.htm#'+anchorPraser()['room_id']+'@';
			viewBtn.innerHTML = '查看投票结果';
			//allowedMsg.style.display = 'block';
			//forbidenMsg.style.display = 'none';
		}else{
			viewBtn.className += ' disabled';
			viewBtn.innerHTML = '投票结果不可看';
			//forbidenMsg.style.display = 'block';
			//allowedMsg.style.display = 'none';
		}

		msgBox.pop({
			title:'投票成功',
			content:'感谢您的一票',
			buttons:[
				{text:'查看结果',link:'result.htm#'+anchorPraser()['room_id']+'@'}
			],
			icon:'success',
		});
	});
}

